<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Think\Page;
class PointController extends AdminController{
	//空操作
	public function _empty(){
		header("HTTP/1.0 404 Not Found");
		$this->display('Public:404');
	}
	public function _initialize(){
		parent::_initialize();
		$point_list=M('Point_config')->select();
		$this->assign('point_list',$point_list);
		$currency_list=M('Currency')->where('is_line = 1')->select();
		$this->assign('currency_list',$currency_list);
	}
	public function index(){
		 	$addTime = I('addTime');
            $endTime = I('endTime');
            $addTime = str_replace('+',' ',$addTime );
            $endTime = str_replace('+',' ',$endTime );
            $addTime=empty($addTime)?0:strtotime($addTime);//开始时间
            $endTime=empty($endTime)?0:strtotime($endTime);//结束时间
            if(!empty($addTime) && empty($endTime)){
                $where["a.add_time"] = array('egt',$addTime);
            }else if (empty($addTime) && !empty($endTime)){
                $where["a.add_time"] = array('elt',$endTime);
            }else if (!empty($addTime) && !empty($endTime)){
                $where["a.add_time"] = array('between',$addTime.','.$endTime);
            }else {
                $where = array();
            }
			$currency=M('Currency')->where('is_line = 1')->select();
            $type=I('type');
			$currency_id=I('currency_id');
			$email=I('email');
			$point_type=I('point_type');
        	if(!empty($type)){
                $where['a.type'] = array("EQ",$type);
            }
            if(!empty($point_type)){
            	$where['a.point_type'] = array("EQ",$point_type);
            }
            if(!empty($currency_id)){
//                 $where['a.currency_id'] = array("EQ",$currency_id);
				$c_id=$currency_id;
            }else{
            	$c_id=$currency[0]['currency_id'];
            	
            }
            $where['a.currency_id']=$c_id;
           // dump($c_id);
            if(!empty($email)){
                $where['c.email'] = array('like',"%".$email."%");
            }
          
        $field = "a.id,a.buy_point,a.money,a.point_type,a.type,a.add_time,a.status,b.currency_name as b_name,c.email as email";
        $count = M('Point_orders')
            ->alias('a')
            ->field($field)
            ->join("LEFT JOIN ".C("DB_PREFIX")."currency AS b ON a.currency_id = b.currency_id")
            ->join("LEFT JOIN ".C("DB_PREFIX")."member as c on a.member_id = c.member_id ")
            ->where($where)
            ->count();// 查询满足要求的总记录数
        $Page       = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        //给分页传参数
        setPageParameter($Page, array('type'=>$type,'point_type'=>$point_type,'currency_id'=>$currency_id,'email'=>$email,'addTime'=>I('addTime'),'endTime'=>I('endTime')));
         
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('Point_orders')
            ->alias('a')
            ->field($field)
            ->join("LEFT JOIN ".C("DB_PREFIX")."currency AS b ON a.currency_id = b.currency_id")
            ->join("LEFT JOIN ".C("DB_PREFIX")."member as c on a.member_id = c.member_id ")
            ->where($where)
            ->order(" a.add_time desc ")
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        if($list){
            foreach ($list as $key=>$vo) {
                $list[$key]['type_name'] = getOrdersType($vo['type']);
                $list[$key]['point_name'] =M('Point_config')->where('id ='.$vo['point_type'])->find()['point'];
            }
        }
        //币种
       
       
      
        $this->assign('currency',$currency);
        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign("c_id",$c_id);
        $this->display();
	}
	
	public function finish(){
		$currency=M('Currency')->where('is_line = 1')->select();
            $type=I('type');
			$currency_id=I('currency_id');
			$email=I('email');
			$point_type=I('point_type');
        	if(!empty($type)){
                $where['a.type'] = array("EQ",$type);
            }
            if(!empty($point_type)){
            	$where['a.point_type'] = array("EQ",$point_type);
            }
            if(!empty($currency_id)){
//                 $where['a.currency_id'] = array("EQ",$currency_id);
				$c_id=$currency_id;
            }else{
            	$c_id=$currency[0]['currency_id'];
            	
            }
            $where['a.currency_id']=$c_id;
            $where['a.status']=1;
           // dump($c_id);
            if(!empty($email)){
                $where['c.email'] = array('like',"%".$email."%");
            }
          
        $field = "a.id,a.buy_point,a.money,a.point_type,a.type,a.add_time,a.status,b.currency_name as b_name,c.email as email";
        $count = M('Point_orders')
            ->alias('a')
            ->field($field)
            ->join("LEFT JOIN ".C("DB_PREFIX")."currency AS b ON a.currency_id = b.currency_id")
            ->join("LEFT JOIN ".C("DB_PREFIX")."member as c on a.member_id = c.member_id ")
            ->where($where)
            ->count();// 查询满足要求的总记录数
        $Page       = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        //给分页传参数
        setPageParameter($Page, array('type'=>$type,'point_type'=>$point_type,'currency_id'=>$currency_id,'email'=>$email,'addTime'=>I('addTime'),'endTime'=>I('endTime')));
         
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('Point_orders')
            ->alias('a')
            ->field($field)
            ->join("LEFT JOIN ".C("DB_PREFIX")."currency AS b ON a.currency_id = b.currency_id")
            ->join("LEFT JOIN ".C("DB_PREFIX")."member as c on a.member_id = c.member_id ")
            ->where($where)
            ->order(" a.add_time desc ")
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        if($list){
            foreach ($list as $key=>$vo) {
                $list[$key]['type_name'] = getOrdersType($vo['type']);
                $list[$key]['point_name'] =M('Point_config')->where('id ='.$vo['point_type'])->find()['point'];
            }
        }
        //币种
       
       
      
        $this->assign('currency',$currency);
        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign("c_id",$c_id);
        $this->display();
	}
	
	public function unfinish(){
		$currency=M('Currency')->where('is_line = 1')->select();
            $type=I('type');
			$currency_id=I('currency_id');
			$email=I('email');
			$point_type=I('point_type');
        	if(!empty($type)){
                $where['a.type'] = array("EQ",$type);
            }
            if(!empty($point_type)){
            	$where['a.point_type'] = array("EQ",$point_type);
            }
            if(!empty($currency_id)){
//                 $where['a.currency_id'] = array("EQ",$currency_id);
				$c_id=$currency_id;
            }else{
            	$c_id=$currency[0]['currency_id'];
            	
            }
            $where['a.currency_id']=$c_id;
            $where['a.status']=0;
           // dump($c_id);
            if(!empty($email)){
                $where['c.email'] = array('like',"%".$email."%");
            }
          
        $field = "a.id,a.buy_point,a.money,a.point_type,a.type,a.add_time,a.status,b.currency_name as b_name,c.email as email";
        $count = M('Point_orders')
            ->alias('a')
            ->field($field)
            ->join("LEFT JOIN ".C("DB_PREFIX")."currency AS b ON a.currency_id = b.currency_id")
            ->join("LEFT JOIN ".C("DB_PREFIX")."member as c on a.member_id = c.member_id ")
            ->where($where)
            ->count();// 查询满足要求的总记录数
        $Page       = new Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        //给分页传参数
        setPageParameter($Page, array('type'=>$type,'point_type'=>$point_type,'currency_id'=>$currency_id,'email'=>$email,'addTime'=>I('addTime'),'endTime'=>I('endTime')));
         
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('Point_orders')
            ->alias('a')
            ->field($field)
            ->join("LEFT JOIN ".C("DB_PREFIX")."currency AS b ON a.currency_id = b.currency_id")
            ->join("LEFT JOIN ".C("DB_PREFIX")."member as c on a.member_id = c.member_id ")
            ->where($where)
            ->order(" a.add_time desc ")
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        if($list){
            foreach ($list as $key=>$vo) {
                $list[$key]['type_name'] = getOrdersType($vo['type']);
                $list[$key]['point_name'] =M('Point_config')->where('id ='.$vo['point_type'])->find()['point'];
            }
        }
        //币种
       
       
      
        $this->assign('currency',$currency);
        $this->assign('list',$list);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign("c_id",$c_id);
        $this->display();
	}
}